package com.bkgy.modular.tbdsjrawcheckc642.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.calsidata.entity.TbFalt;
import com.bkgy.modular.datacollect_bx.bean.JDataSave622;
import com.bkgy.modular.tbdsjrawcheckc642.dao.AccuracyMapper;
import com.bkgy.modular.tbdsjrawcheckc642.entity.Accuracy;
import com.bkgy.modular.tbdsjrawcheckc642.service.AccuracyService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccuracyServiceImpl extends ServiceImpl<AccuracyMapper, Accuracy> implements AccuracyService {

    @Resource
    private MongoTemplate mongoTemplate;
    @Override
    public ResultData findByPopup(JSONObject json) {
        // 获取前端传过来的extid
        String extid = json.getString("extid");

        // 判断extid是否为空或者null
        if (StringUtils.hasText(extid)) {
            // 创建查询条件
            Query query = new Query(Criteria.where("extid").is(extid));
            List<Accuracy> list = mongoTemplate.find(query, Accuracy.class);
            List<TbFalt> tbFalts = mongoTemplate.find(query, TbFalt.class);
            List<JDataSave622> jDataSave622List = mongoTemplate.find(query, JDataSave622.class);

            Map<String,Object> map = new HashMap<>();
            if(!ObjectUtils.isEmpty(jDataSave622List.get(0))&&!ObjectUtils.isEmpty(jDataSave622List.get(0).getRow().get("c622_tcm_control_guagedev_thk"))){
                for (JDataSave622 jDataSave622 : jDataSave622List) {
                    Map<String, List<Double>> edge = jDataSave622.getRow();
                    Object c622_tcm_exs5_guagedev_thk1 = edge.get("c622_tcm_exs5_guagedev_thk1");
                    Object c622_tcm_exs5_thk1 = edge.get("c622_tcm_exs5_thk1");
                    Object c622_tcm_exs5_speed = edge.get("c622_tcm_exs5_speed");
                    Object c622_tcm_exs5_guagedev_thk2 = edge.get("c622_tcm_exs5_guagedev_thk2");
                    Object c622_tcm_control_guagedev_thk = edge.get("c622_tcm_control_guagedev_thk");
                    Object c622_tcm_exs5_thk2 = edge.get("c622_tcm_exs5_thk2");
                    map.put("c622_tcm_exs5_guagedev_thk1",c622_tcm_exs5_guagedev_thk1);
                    map.put("c622_tcm_exs5_thk1",c622_tcm_exs5_thk1);
                    map.put("c622_tcm_exs5_speed",c622_tcm_exs5_speed);
                    map.put("c622_tcm_exs5_guagedev_thk2",c622_tcm_exs5_guagedev_thk2);
                    map.put("c622_tcm_control_guagedev_thk",c622_tcm_control_guagedev_thk);
                    map.put("c622_tcm_exs5_thk2",c622_tcm_exs5_thk2);
                }
            }else {
                for (TbFalt tbFalt : tbFalts) {
                    Map<String, Object> edge = tbFalt.getEdge();
                    Object c622_tcm_exs5_guagedev_thk1 = edge.get("c622_tcm_exs5_guagedev_thk1");
                    Object c622_tcm_exs5_thk1 = edge.get("c622_tcm_exs5_thk1");
                    Object c622_tcm_exs5_speed = edge.get("c622_tcm_exs5_speed");
                    Object c622_tcm_exs5_guagedev_thk2 = edge.get("c622_tcm_exs5_guagedev_thk2");
                    Object c622_tcm_control_guagedev_thk = edge.get("c622_tcm_control_guagedev_thk");
                    Object c622_tcm_exs5_thk2 = edge.get("c622_tcm_exs5_thk2");
                    map.put("c622_tcm_exs5_guagedev_thk1",c622_tcm_exs5_guagedev_thk1);
                    map.put("c622_tcm_exs5_thk1",c622_tcm_exs5_thk1);
                    map.put("c622_tcm_exs5_speed",c622_tcm_exs5_speed);
                    map.put("c622_tcm_exs5_guagedev_thk2",c622_tcm_exs5_guagedev_thk2);
                    map.put("c622_tcm_control_guagedev_thk",c622_tcm_control_guagedev_thk);
                    map.put("c622_tcm_exs5_thk2",c622_tcm_exs5_thk2);
                }
            }



            for (Accuracy item : list) {
                map.put("extid",item.getExtid());
                map.put("thk_Head_Tole_Pos",item.getThk_Head_Tole_Pos());
                map.put("thk_Mid_Tole_Pos",item.getThk_Mid_Tole_Pos());
                map.put("thk_Tail_Tole_Pos",item.getThk_Tail_Tole_Pos());
                map.put("thk_Whole_Tole_Pos",item.getThk_Whole_Tole_Pos());
            }
            return ResultData.ResultDataFaultSelf("操作成功",map);
        } else {
            return ResultData.ResultDataFaultSelf("数据为空",null);
        }
    }
}
